/**
* @ProjectName: ShopSytem
* @Title: AdminAddGood.java 
* @Package com.element5th.shop.servlet.good 
* @Description: 后台进行添加商品操作
* @author 第五元素小组   
* @date 2012-10-26 16:22:05 
* @version V1.0   
*/
package com.element5th.shop.servlet.good;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.catalina.core.ApplicationPart;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.element5th.comm.ServletUtil;
import com.element5th.shop.comm.DaoFactory;
import com.element5th.shop.dao.good.IAdminGoodDao;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.entity.DetailPic;

/**
 * Servlet implementation class AddGood
 */
@WebServlet("/page/admin/page/add_good")
@MultipartConfig(fileSizeThreshold=10*1024*1024,
	maxFileSize=1*1024*1024, maxRequestSize=20*1024*1024)
public class AdminAddGood extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String msg = null;
		String path = "/page/admin/page/add_good.jsp";
		
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if(isMultipart) {
			/*提取出各项信息*/
			BaseDto dto = new BaseDto();
			
			int classId = ServletUtil.getInteger(request, "class");
			String goodName = request.getParameter("name");
			String goodBrand = request.getParameter("brand");
			Double marketPrice = ServletUtil.getDouble(request, "marketPrice");
			double price = ServletUtil.getDouble(request, "price");
			
			dto.getShopGood().setClassId(classId);
			dto.getShopGood().setGoodName(goodName);
			dto.getShopGood().setGoodBrand(goodBrand);
			dto.getShopGood().setGoodMarketprice(marketPrice);
			dto.getShopGood().setGoodPrice(price);
			
			//保存商品信息，并获得商品ID
			IAdminGoodDao dao = DaoFactory.getAdminGoodDao();
			int goodId = dao.addGood(dto);
			
			dto.getGoodDetail().setGoodId(goodId);
			
			String detailTitle = request.getParameter("title");
			String detailDesc = request.getParameter("desc");
			
			dto.getGoodDetail().setDetailTitle(detailTitle);
			dto.getGoodDetail().setDetailDesc(detailDesc);
			
			/*取出上传文件信息*/
			int detailPicCount = 0;
			for (Part part : request.getParts()) {
				ApplicationPart appPart = (ApplicationPart) part;
				String type = appPart.getContentType();
				/*部署前应改为此路径*/
				//String filePath = request.getSession().getServletContext().getRealPath("/images/goodpic/");
				String filePath = "E:\\workspace_web\\ShopSytem\\Web\\images\\goodPic";
				if(type != null && type.contains("image/jpeg")){
					String paramName = appPart.getName();
					String fileName = null;
					if(paramName.equals("iconPic")) {
						if(appPart.getSize() == 0){
							msg = "图标文件不正确！";
							break;
						}
						fileName = "goodIcon_" + goodId + ".jpg";
						appPart.write(filePath + 
								"\\orderpic\\" + fileName);
						appPart.delete();
					} else if(paramName.equals("topPic")) {
						if(appPart.getSize() == 0){
							msg = "商品图片文件不正确！";
							break;
						}
						fileName = "goodPic_" + goodId + ".jpg";
						dto.getGoodDetail().setDetailToppic(fileName);
						appPart.write(filePath + 
								"\\toppic\\" + fileName);
						appPart.delete();
					} else {
						if(appPart.getSize() > 0){
							detailPicCount++;
							fileName = "goodPic_" + goodId + "_" + detailPicCount + ".jpg";
							DetailPic detailPic = new DetailPic();
							detailPic.setPicUrl(fileName);
							dto.getGoodDetail().getDetailPics().add(detailPic);
							appPart.write(filePath + 
									"\\detail\\" + fileName);
							System.out.println(filePath + 
									"\\detail\\" + fileName);
							appPart.delete();
						}
					}
				}
			}
			boolean result = dao.addGoodDeatail(dto);
			
			if(result)
				msg = "添加成功！";
			else
				msg = "添加失败！";
		} else {
			msg = "错误的提交方式！";
		}
		
		request.setAttribute("msg", msg);
		this.getServletContext().getRequestDispatcher(path).forward(request, response);
	}

}
